package com.hsj.springai.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
    import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Share1 {
    private static final Logger log = LoggerFactory.getLogger(Share1.class);
    private int number = 0;

    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    public void incr() throws InterruptedException {
        try {
            lock.lock();
            while (number != 0) {
                condition.await();
            }
            number++;
            System.out.println(Thread.currentThread().getName() + ":" + number);
            condition.signalAll();
        } finally {
            lock.unlock();
        }
    }

    public void decr() throws InterruptedException {
        try {
            lock.lock();
            while (number != 1) {
                condition.await();
            }
            number--;
            System.out.println(Thread.currentThread().getName() + ":" + number);
            condition.signalAll();
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        //第三步 创建多线程 调用 操作方法
        Share1 share = new Share1();
        new Thread(() -> {
            for (int i = 1; i <= 10; i++) {
                try {
                    share.incr();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, "AA").start();

        new Thread(() -> {
            for (int i = 1; i <= 10; i++) {
                try {
                    share.decr();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, "BB").start();

        new Thread(() -> {
            for (int i = 1; i <= 10; i++) {
                try {
                    share.incr();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, "CC").start();

        new Thread(() -> {
            for (int i = 1; i <= 10; i++) {
                try {
                    share.decr();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, "DD").start();
    }
}

class DeadLock {
    static Object a = new Object();
    static Object b = new Object();

}

 class DateValidator {
    public static void main(String[] args) {
        // 要校验的日期字符串
        String dateString = "2024-01-02";

        // 定义正则表达式
        String regex = "^2024-01-01$";

        // 编译正则表达式
        Pattern pattern = Pattern.compile(regex);

        // 创建 Matcher 对象
        Matcher matcher = pattern.matcher(dateString);

        // 进行匹配
        boolean isMatch = matcher.matches();

        // 输出结果
        if (isMatch) {
            System.out.println("日期格式正确");
        } else {
            System.out.println("日期格式错误");
        }
    }
}
